package cn.starfire.sttool.tools;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

import java.security.SecureRandom;
/**
 * @description:
 * @author: tanpengji
 * @time: 2025/8/27 10:05
 */
public class PasswordGeneratorTool implements ToolModule {

    @Override
    public String getName() {
        return "随机密码生成";
    }

    @Override
    public VBox getContent() {
        // 密码长度输入
        Label lenLabel = new Label("密码长度：");
        lenLabel.setFont(Font.font(14));
        TextField lengthField = new TextField("12");
        lengthField.setPrefWidth(60);
        lengthField.setFont(Font.font(14));
        HBox lengthBox = new HBox(10, lenLabel, lengthField);
        lengthBox.setAlignment(Pos.CENTER_LEFT);

        // 字符类型和具体内容
        CheckBox includeUpper = new CheckBox("包含大写字母");
        Label upperLabel = new Label(" (A-Z)");
        includeUpper.setSelected(true);

        CheckBox includeLower = new CheckBox("包含小写字母");
        Label lowerLabel = new Label(" (a-z)");
        includeLower.setSelected(true);

        CheckBox includeNum = new CheckBox("包含数字");
        Label numLabel = new Label(" (0-9)");
        includeNum.setSelected(true);

        CheckBox includeSymbol = new CheckBox("包含特殊符号");
        Label symbolLabel = new Label(" (!@#$%^&*()-_=+)");
        includeSymbol.setSelected(true);

        HBox upperBox = new HBox(5, includeUpper, upperLabel);
        HBox lowerBox = new HBox(5, includeLower, lowerLabel);
        HBox numBox = new HBox(5, includeNum, numLabel);
        HBox symbolBox = new HBox(5, includeSymbol, symbolLabel);

        // 生成按钮
        Button genBtn = new Button("生成密码");
        genBtn.setStyle("-fx-background-color: #4CAF50; -fx-text-fill: white; -fx-font-weight: bold;");
        genBtn.setPrefWidth(120);
        genBtn.setFont(Font.font(14));

        // 密码显示
        TextField resultField = new TextField();
        resultField.setEditable(false);
        resultField.setFont(Font.font("Consolas", 16));
        resultField.setStyle("-fx-font-weight: bold; -fx-text-fill: #333;");

        // 显示每种类型是否包含
        HBox infoBox = new HBox(20);
        infoBox.setAlignment(Pos.CENTER_LEFT);
        Label upperInfo = new Label("大写字母: ×"); upperInfo.setTextFill(Color.RED);
        Label lowerInfo = new Label("小写字母: ×"); lowerInfo.setTextFill(Color.RED);
        Label numInfo = new Label("数字: ×"); numInfo.setTextFill(Color.RED);
        Label symbolInfo = new Label("特殊符号: ×"); symbolInfo.setTextFill(Color.RED);
        infoBox.getChildren().addAll(upperInfo, lowerInfo, numInfo, symbolInfo);

        // 按钮事件
        genBtn.setOnAction(e -> {
            int len;
            try {
                len = Integer.parseInt(lengthField.getText());
            } catch (NumberFormatException ex) {
                resultField.setText("请输入合法数字");
                return;
            }

            String password = generatePassword(len,
                    includeUpper.isSelected(),
                    includeLower.isSelected(),
                    includeNum.isSelected(),
                    includeSymbol.isSelected());
            resultField.setText(password);

            // 更新高亮显示
            upperInfo.setText(password.matches(".*[A-Z].*") ? "大写字母: √" : "大写字母: ×");
            upperInfo.setTextFill(password.matches(".*[A-Z].*") ? Color.GREEN : Color.RED);

            lowerInfo.setText(password.matches(".*[a-z].*") ? "小写字母: √" : "小写字母: ×");
            lowerInfo.setTextFill(password.matches(".*[a-z].*") ? Color.GREEN : Color.RED);

            numInfo.setText(password.matches(".*[0-9].*") ? "数字: √" : "数字: ×");
            numInfo.setTextFill(password.matches(".*[0-9].*") ? Color.GREEN : Color.RED);

            symbolInfo.setText(password.matches(".*[!@#$%^&*()\\-_=+].*") ? "特殊符号: √" : "特殊符号: ×");
            symbolInfo.setTextFill(password.matches(".*[!@#$%^&*()\\-_=+].*") ? Color.GREEN : Color.RED);
        });

        VBox root = new VBox(15,
                lengthBox,
                upperBox, lowerBox, numBox, symbolBox,
                genBtn,
                new Label("生成结果："), resultField,
                infoBox
        );
        root.setPadding(new Insets(20));
        root.setStyle("-fx-background-color: #f9f9f9; -fx-border-color: #ccc; -fx-border-radius: 5px; -fx-background-radius: 5px;");

        return root;
    }

    private String generatePassword(int length, boolean upper, boolean lower, boolean num, boolean symbol) {
        String upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String lowerChars = "abcdefghijklmnopqrstuvwxyz";
        String numbers = "0123456789";
        String symbols = "!@#$%^&*()-_=+";

        StringBuilder pool = new StringBuilder();
        if (upper) pool.append(upperChars);
        if (lower) pool.append(lowerChars);
        if (num) pool.append(numbers);
        if (symbol) pool.append(symbols);

        if (pool.length() == 0) return "请至少选择一个字符集";

        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(pool.charAt(random.nextInt(pool.length())));
        }
        return sb.toString();
    }
}
